//
//  OKSickCaseController.m
//  ZLFastDoctor
//
//  Created by answer on 16/5/25.
//  Copyright © 2016年 LS. All rights reserved.
//

#import "OKSickCaseController.h"
#import "OKSickCaseView.h"
#import <Masonry/Masonry.h>
#import "OKSickCaseButton.h"
#import "NQRecordManageVc.h"
#import "OKCommitView.h"
#import "UIView+XYFrame.h"
#import "OKCommitView.h"
#import "OKSickCasePushController.h"
#import "OKSickCaseView.h"
#import "NQRecord.h"

@interface OKSickCaseController () <OKCommitViewDelegate, OKSickCaseViewDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate>
@property (strong, nonatomic) UIScrollView *scrollView;
@property (strong, nonatomic) OKSickCaseView *topView;
@property (strong, nonatomic) UITextView *textView;
@property (strong, nonatomic) UIButton *sureButton;
@property (strong, nonatomic) OKCommitView *commitView;
@property (strong, nonatomic) UIView *coverView;
@property (strong, nonatomic) UIButton *addImageBtn;

@end

@implementation OKSickCaseController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    self.view.backgroundColor = [UIColor whiteColor];
    //self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone
    self.title = @"递交资料";
}

- (void)setupUI {

    [self.view addSubview:self.scrollView];
    [self.scrollView addSubview:self.topView];
    
    __weak typeof(self) weakself = self;
    [self.topView setCallBack:^{
        NQRecordManageVc *recordVc = [[NQRecordManageVc alloc] init];
        
        //解档病例数据源
        NSArray *recordArr = [NQRecord readRecordArr];
        //顺传
        if (recordArr) {
            recordVc.recordData = recordArr;
        }
        
        //解档用户基本信息
        NQUserInfo *userInfo = [NQUserInfo readUserInfo];
        if (userInfo) {
            recordVc.userInfo = userInfo;
        }

        recordVc.superVc  =  weakself;
        
        [weakself.navigationController pushViewController:recordVc animated:YES];
    }];
    
    [self.view addSubview:self.sureButton];
    
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.equalTo(self.scrollView);
        make.width.equalTo(self.view);
        make.height.equalTo(@728);
    }];
    
    [self.sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(10);
        make.right.equalTo(self.view).offset(-10);
        make.bottom.equalTo(self.view).offset(-5);
        make.height.equalTo(@44);
    }];

}


- (OKSickCaseView *)topView {
    if (_topView == nil) {
        _topView = [OKSickCaseView sickCaseView];
        _topView.delegate = self;
    }
    return _topView;
}

#pragma mark - 点击添加病例图片按钮回调

- (void)btnAddImageCallBack:(UIButton *)btn {
    self.addImageBtn = btn;
    UIImagePickerController *vc = [[UIImagePickerController alloc] init];
    //设置跳转类型 -- 所有相册
    //vc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    //设置代理
    vc.delegate = self;
    //设置选择后的图片可被编辑
    //vc.allowsEditing = YES;
    
    [self.navigationController presentViewController:vc animated:YES completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary<NSString *,id> *)editingInfo {
    [self.addImageBtn setBackgroundImage:image forState:UIControlStateNormal];
}

//- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
//    
//}


- (UIScrollView *)scrollView {
    if (_scrollView == nil) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.showsVerticalScrollIndicator = NO;
    }
    return _scrollView;
}

- (UIButton *)sureButton {
    if (_sureButton == nil) {
        _sureButton = [[UIButton alloc] init];
        [_sureButton setTitle:@"确定" forState:UIControlStateNormal];
        [_sureButton setBackgroundColor:[UIColor colorWithRed:50/255.0 green:190/255.0 blue:190/255.0 alpha:1.0]];
        _sureButton.layer.cornerRadius = 5;
        [_sureButton addTarget:self action:@selector(sureButtonClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sureButton;
}

- (void)sureButtonClick {
    
    [self.view addSubview:self.coverView];
    [self.view addSubview:self.commitView];
    self.coverView.frame = [UIScreen mainScreen].bounds;
    
    self.commitView.bounds = CGRectMake(0, 0, 300 , 350);
    self.commitView.center = self.view.center;
    self.commitView.y = 100;
//    
    self.commitView.transform = CGAffineTransformMakeScale(0.1, 0.1);
//
    [UIView animateWithDuration:1.0 animations:^{
        self.commitView.transform = CGAffineTransformMakeScale(1.0, 1.0);
    }];
    
}

- (OKCommitView *)commitView {
    if (_commitView == nil) {
        _commitView = [OKCommitView commitView];
        _commitView.delegate = self;
    }
    return _commitView;
}

- (void)commitViewCallBackWith:(UIButton *)btn {
    if (btn.tag == 1) {
            self.commitView.transform = CGAffineTransformMakeScale(1, 1);
        [UIView animateWithDuration:1.0 animations:^{
            self.commitView.transform = CGAffineTransformMakeScale(0.1, 0.1);
        }completion:^(BOOL finished) {
            [self removeView];
        }];
        
    } else {
        OKSickCasePushController *vc = [[OKSickCasePushController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
        [self removeView];
    }
}

- (void)removeView {
    [self.commitView removeFromSuperview];
    [self.coverView removeFromSuperview];
    self.commitView = nil;
    self.coverView = nil;
}

- (UIView *)coverView {
    if (_coverView == nil) {
        _coverView = [[UIView alloc] init];
        _coverView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
        _coverView.frame = [UIScreen mainScreen].bounds;
    }
    return _coverView;
}


@end
